<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adjacency_xml {
	
	private $CI;
	private $rs 		= '';
	private $fields 	= array();
	private $leafnodes 	= array();
	private $id 		= '';
	private $level 		= '';
		
	function __construct($params = array())
	{
		$this->CI =& get_instance();
		
		if (count($params) > 0)
		{
			$this->initialize($params);		
		}
	}
	
	function initialize($params = array())
	{
		foreach ($params as $key => $val)
		{
			if (isset($this->$key))
			{
				$this->$key = $val;
			}
		}
	}
	
	function _run()
	{
		$this->fields = $this->rs->field_data();
		return $this->_xml_encode();
	}
	
	function _xml_encode()
	{
		$xml = '<?xml version="1.0" encoding="utf-8"?>';
		$xml .= '<rows>';
		$xml .= '<page>1</page>';
		$xml .= '<total>1</total>';
		$xml .= '<records>1</records>';
		
		$result = $this->rs->result();
		
		foreach ($result as $row)
		{
			$xml .= '<row>';
			foreach ($this->fields as $cols)
			{
				$field = $cols->name;
				
				if ($field != 'parent_id')
				{
					$xml .= '<cell>'. $row->$field.'</cell>';
				}
			}
			
			$xml .= '<cell>'. $this->level .'</cell>';
			
			if ( ! $row->parent_id) $parent_id = 'NULL'; 
			else $parent_id = $row->parent_id; 
			
			$xml .= '<cell><![CDATA['. $parent_id .']]></cell>';
			
			$id = $this->id;
			if (isset($this->leafnodes[$row->$id]))
			{
				$leaf = ($row->$id == $this->leafnodes[$row->$id]) ? 'true' : 'false';
			}
			else
			{
				$leaf = 'false';
			}
			
			$xml .= '<cell>'. $leaf .'</cell>';
			$xml .= '<cell>false</cell>';
			
			$xml .= '</row>';
		}
		
		$xml .= '</rows>';
		
		return $xml;
	}
	
}
/* End of file Adjacency_xml.php */
/* Location: ./application/libraries/Adjacency_xml.php */
